<?php

namespace App\Http\Controllers\Admin\Ca;

use App\Http\Controllers\Admin\BaseController;
use App\Http\Requests\Admin\Ca\PromotionSaveRequest;
use App\Repositories\Ca\PromotionRepository;
use App\Utils\Output;
use App\Utils\ViewHelper;
use Illuminate\Http\Request;
use App\Facade\AppContext;

class PromotionController extends BaseController
{
    public function __construct()
    {
        $this->set('ca/promotion');
    }

    function index()
    {
        return $this->view('index');
    }

    function pager(PromotionRepository $rep, Request $request)
    {
        return $rep->adminPager($request->all());
    }

    function create()
    {
        return $this->view('create',['user_id'=>AppContext::userId(),'hospital_id'=>AppContext::hospitalId()]);
    }

    function save(PromotionSaveRequest $request, PromotionRepository $repository): Output
    {
        $data = $request->all();
        return $repository->save($data, 0);
    }

    function update(PromotionSaveRequest $request, PromotionRepository $repository): Output
    {
        $data = $request->all();
        $id = $request->get('id', 0);
        return $repository->save($data, $id);
    }

    function edit($id, PromotionRepository $repository)
    {
        $result = $repository->detail($id);
        if ($result->isSuccess()) {
            return $this->view('edit')->with('entity', $result->data);
        }
        return ViewHelper::E404($result->msg);
    }

    function batchDelete(Request $request, PromotionRepository $repository): Output
    {
        return $repository->batchDelete($request->post('ids', ''));
    }

    function delete(Request $request, PromotionRepository $repository): Output
    {
        return $repository->delete($request->post('id', ''));
    }

    function load(Request $request, PromotionRepository $repository): Output
    {
        return $repository->load($request->all());
    }

}